package org.oslims.client.uicomponents.editbox;

import org.oslims.client.uicomponents.inputfields.TextInputField;
import org.oslims.domain.ContactAddress;
import org.oslims.domain.Customer;

import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditCustomerBox extends EditUserBox {

	final DisclosurePanel contactAddressPanel = new DisclosurePanel("Contact Details");
	final VerticalPanel contactAddressGroupPanel = new VerticalPanel();

	final TextInputField streetField = new TextInputField("street", "Street: ", "80px", "200px");
	final TextInputField streetNoField = new TextInputField("streetNo", "Street No: ", "80px", "200px");
	final TextInputField zipCodeField = new TextInputField("zipCode", "Zip Code: ", "80px", "200px");
	final TextInputField cityField = new TextInputField("city", "City: ", "80px", "200px");
	final TextInputField countryField = new TextInputField("country", "Country: ", "80px", "200px");

	private ContactAddress contactAddress;
	private Customer customer;

	public EditCustomerBox() {
		// Call super to generate the default fields for a user
		super();

		contactAddressGroupPanel.add(streetField);
		inputFields.put(streetField.getFieldName(), streetField);

		contactAddressGroupPanel.add(streetNoField);
		inputFields.put(streetNoField.getFieldName(), streetNoField);

		contactAddressGroupPanel.add(zipCodeField);
		inputFields.put(zipCodeField.getFieldName(), zipCodeField);

		contactAddressGroupPanel.add(cityField);
		inputFields.put(cityField.getFieldName(), cityField);

		contactAddressGroupPanel.add(countryField);
		inputFields.put(countryField.getFieldName(), countryField);

		contactAddressPanel.add(contactAddressGroupPanel);

		editUserBoxPanel.add(contactAddressPanel);

		// Add the save and cancel buttons
		editUserBoxPanel.add(buttonLine);

		// Open the address panel
		contactAddressPanel.setOpen(true);

		clearAllErrors();
	}

	@Override
	protected void clear() {
		super.clear();

		streetField.getInputField().setValue("");
		streetNoField.getInputField().setValue("");
		zipCodeField.getInputField().setValue("");
		cityField.getInputField().setValue("");
		countryField.getInputField().setValue("");
		contactAddress = null;
		clearAllErrors();
	}

	public void showNew() {
		this.clear();
		this.clearAllErrors();
		this.customer = null;
		this.contactAddress = null;
		editUserBox.setText("Create new Customer");
		editUserBox.center();
	}

	public void showEdit(Customer customer) {
		this.clear();
		this.clearAllErrors();
		editUserBox.setText("Edit Customer");
		super.showEdit(customer);
		this.customer = customer;

		if (customer.getContactAdress() != null) {
			ContactAddress ca = customer.getContactAdress();
			contactAddress = ca;
			streetField.getInputField().setValue(ca.getStreet());
			streetNoField.getInputField().setValue(ca.getStreetNo().toString());
			zipCodeField.getInputField().setValue(ca.getZipCode());
			cityField.getInputField().setValue(ca.getCity());
			countryField.getInputField().setValue(ca.getCountry());
		}

		editUserBox.center();
	}

	public void showError(String fieldName, String message) {

		super.showError(fieldName, message);
	}

	public void clearError(String fieldName) {

		super.clearError(fieldName);
	}

	public void clearAllErrors() {
		super.clearAllErrors();
	}

	public HasValue<String> getStreet() {
		return streetField.getInputField();
	}

	public HasValue<String> getStreetNo() {
		return streetNoField.getInputField();
	}

	public HasValue<String> getZipCode() {
		return zipCodeField.getInputField();
	}

	public HasValue<String> getCity() {
		return cityField.getInputField();
	}

	public HasValue<String> getCountry() {
		return countryField.getInputField();
	}

	public void setContactAddress(ContactAddress contactAddress) {
		this.contactAddress = contactAddress;
	}

	public ContactAddress getContactAddress() {
		return contactAddress;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Customer getCustomer() {
		return customer;
	}
}
